﻿
using MediatR;
using Production_Set.API.Application.Commands.WorkCenters;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Procurement;
using THE_WIND_RISES.Common.Domans.Produce.Resources;
using THE_WIND_RISES.Common.Domans.Produce.WorkCenter_;
using THE_WIND_RISES.Common.Dtos;


namespace Production_Set.API.Application.Commands.Procurement
{
    public class PurchasingsSelIdHandler : IRequestHandler<PurchasingsSelIdCommand, Result>
    {
        private readonly IBaseRepository<Purchasings> PurchasingsRepository;
        private readonly IBaseRepository<PaymentPlans> PaymentPlansRepository;
        private readonly IBaseRepository<OrderLines> OrderLinesRepository;
        private readonly ILogger<PurchasingsSelIdHandler> logger;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="PurchasingsRepository"></param>
        public PurchasingsSelIdHandler(ILogger<PurchasingsSelIdHandler> logger, IBaseRepository<Purchasings> PurchasingsRepository, IBaseRepository<PaymentPlans> PaymentPlansRepository, IBaseRepository<OrderLines> OrderLinesRepository)
        {
            this.logger = logger;
            this.PurchasingsRepository = PurchasingsRepository;
            this.PaymentPlansRepository = PaymentPlansRepository;
            this.OrderLinesRepository = OrderLinesRepository;
        }
        public async Task<Result> Handle(PurchasingsSelIdCommand request, CancellationToken cancellationToken)
        {
            var result = new THE_WIND_RISES.Common.Dtos.Result() { Code = (int)CommonErrorCodes.Added };
            try
            {
                string purchasingssql = $"select * from Purchasings WHERE PurchasinId = {request.PurchasinId}";
                //string OrderLinessql = $"select * from OrderLines WHERE PurchasinId = {request.PurchasinId}";
                //string PaymentPlanssql = $"select * from PaymentPlans WHERE PurchasinId = {request.PurchasinId}";

                var list = PurchasingsRepository.GetAll(purchasingssql);

                if (list != null)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = list;
                    result.Msg = "反填信息成功";
                    logger.LogInformation($"反填采购/委外订单头信息成功");
                }
                else
                {
                    result.Code = (int)CommonErrorCodes.Search;
                    result.Data = null;
                    result.Msg = "反填信息失败";
                    logger.LogError($"反填采购/委外订单头信息失败");
                }
                //var OrderLinesList = from a in OrderLinesRepository.GetAll(OrderLinessql)
                //                     select a;

                //var PaymentPlansList =from b in PaymentPlansRepository.GetAll(PaymentPlanssql) select b;

                //var data = new
                //{
                //    PaymentPlansList,
                //    OrderLinesList,
                //    list
                //};
                //if (OrderLinesList.Count() > 0 || list.Count() > 0 || PaymentPlansList.Count() > 0)
                //{
                //    result.Code = (int)CommonErrorCodes.Success;
                //    result.Data = data;
                //    result.Msg = "反填信息成功";
                //    logger.LogInformation($"反填采购/委外订单头信息成功");
                //}
                //else
                //{
                //    result.Code = (int)CommonErrorCodes.Search;
                //    result.Data = null;
                //    result.Msg = "反填信息失败";
                //    logger.LogError($"反填采购/委外订单头信息失败");
                //}
            }
            catch (Exception ex)
            {
                logger.LogError($"反填采购/委外订单头信息失败,错误信息为：" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
